import 'dart:io';

import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/ui/patrol/patrol_list_fragment.dart';
import 'package:ls_work/ui/patrol/patrol_record_list_page.dart';
import 'package:ls_work/util/ext_utils.dart';

import '../../viewmodel/patrol/patrol_list_home_view_model.dart';

class PatrolListHomePage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _PatrolListHomeState();
}

class _PatrolListHomeState
    extends BaseMvvmState<PatrolListHomeViewModel, PatrolListHomePage> with TickerProviderStateMixin {
  var timeFormat = [HH, ":", nn];

  @override
  Widget buildLoadingContentView() {
    return viewModel.controller==null?Container(): Column(
      children: [
        TabBar(
            isScrollable: true,
            indicatorColor: Colours.app_main,
            unselectedLabelColor: Colors.black,
            labelColor: Colours.app_main,
            controller: viewModel.controller,
            labelPadding: EdgeInsets.symmetric(horizontal: 10),
            indicatorSize: TabBarIndicatorSize.label,
            tabs: viewModel.taskList
                .map((e) => Tab(
              height: 45,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    e.name??"",
                    style: TextStyle(fontSize: 15),
                  ),
                  Text(
                    "${e.startDate?.formatDateStr(format: timeFormat)}-${e.endDate?.formatDateStr(format: timeFormat)}",
                    style: TextStyle(fontSize: 14),
                  )
                ],
              ),
              iconMargin: EdgeInsets.zero,
            ))
                .toList()),
        TabBarView(
          controller: viewModel.controller,
          children: viewModel.taskList
              .map((e) => PatrolListFragment(
            task: e,
          ))
              .toList(),
        ).setWeight(1),
        Row(
          children: [
            if (Platform.isIOS) ...[
              ElevatedButton(
                onPressed: () {
                  viewModel.startNfc();
                },
                child: CommonText('读取NFC',
                    textSize: 17, textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor:
                    WidgetStateProperty.all(Colours.app_main),
                    minimumSize: WidgetStateProperty.all(
                        Size(double.infinity, 49))),
              ).setWeight(1),
              Container(
                width: 10,
              )
            ],
            ElevatedButton(
              onPressed: () {
                viewModel.scanCode();
              },
              child:
              CommonText('扫码', textSize: 17, textColor: Colors.white),
              style: ButtonStyle(
                  backgroundColor:
                  WidgetStateProperty.all(Colours.app_main),
                  minimumSize:
                  WidgetStateProperty.all(Size(double.infinity, 49))),
            ).setWeight(1)
          ],
        ).addToContainer(
            margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10))
      ],
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Color(0xfff1f1f1),
      appBar: AppBar(
        title: Text('今日巡防'),
        actions: [
          CommonText('巡防记录', textColor: Colours.app_main)
              .addToContainer(
                  alignment: Alignment.center,
                  padding: EdgeInsets.symmetric(horizontal: 16))
              .onTap(() {
            Go().push(PatrolRecordListPage());
          })
        ],
      ),
      body: Column(
        children: [
          topUnPatrolWidget(),
          Stack(
            alignment: Alignment.center,
            children: [
              CommonText(viewModel.selectedArea?.name ?? "",
                  hintText: '选择巡防范围'),
              Icon(Icons.arrow_drop_down).setLocation(right: 0)
            ],
          ).onTap(() {
            viewModel.selectArea();
          }).addToContainer(
              width: double.infinity,
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: 10.borderRadius),
              margin: EdgeInsets.symmetric(horizontal: 16),
              padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10)),
          loadingContentWidget.setWeight(1)
        ],
      ),
    );
  }

  @override
  PatrolListHomeViewModel createViewModel() {
    return PatrolListHomeViewModel(context,vs: this);
  }

  @override
  void onRetryClick() {
    viewModel.getPatrolData();
  }

  Widget topUnPatrolWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        CommonText('今日未打卡巡防点 '),
        CommonText((viewModel.patrolData?.unfinishNum ?? 0).toString(),
                fontWeight: FontWeight.w600, textColor: Colours.app_main)
            .addToContainer(
          alignment: Alignment.center,
          padding: EdgeInsets.all(5),
          decoration: BoxDecoration(
            color: Colors.transparent,
            borderRadius: 20.borderRadius,
            border: Border.all(width: 2, color: Colours.app_main),
          ),
          constraints: BoxConstraints(minHeight: 30, minWidth: 30),
        ),
        CommonText(' 个'),
      ],
    ).addToContainer(
        decoration:
            BoxDecoration(color: Colors.white, borderRadius: 8.borderRadius),
        padding: EdgeInsets.all(8),
        margin: EdgeInsets.symmetric(horizontal: 16, vertical: 10));
  }
}
